//
//  YMMainPrivacyAlert.m
//  SmartCar
//
//  Created by y s on 2023/3/30.
//

#import "YMMainPrivacyAlert.h"
#import "YMProtocolViewController.h"


#define kshow_privacy_alert_key @"kshow_privacy_alert_key"

@interface YMMainPrivacyAlert ()

@property (nonatomic, strong) UIView *vAlert;
@property (nonatomic, strong) UIImageView *imgvTop;
@property (nonatomic, strong) UILabel *lblTitle;
@property (nonatomic, strong) YYLabel *lblContent;

@property (nonatomic, strong) UIButton *btnCancel;
@property (nonatomic, strong) UIButton *btnSure;

@end

@implementation YMMainPrivacyAlert


+ (void)checkShowPrivacy{
    if ([SLocalUtil hasKey:kshow_privacy_alert_key] == NO){
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        YMMainPrivacyAlert *alert = [[YMMainPrivacyAlert alloc] init];
        [alert show:window];
        
    }
}

- (instancetype)init{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark -- UI
- (void)setUI{
    self.backgroundColor = [SUIUtil colorOf:0x000000 alpha:0.55];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
    [self addSubview:self.vAlert];
    [self.vAlert addSubview:self.imgvTop];
    [self.vAlert addSubview:self.lblTitle];
    [self.vAlert addSubview:self.lblContent];
    [self.vAlert addSubview:self.btnSure];
    [self.vAlert addSubview:self.btnCancel];
    
    [self.vAlert mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo([SFitUtil fit:330]);
    }];
    [self.imgvTop mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo([SFitUtil fit:140]);
        make.height.mas_equalTo([SFitUtil fit:143.5]);
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo([SFitUtil fit:20]);
    }];
    [self.lblTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:20]);
        make.right.mas_equalTo([SFitUtil fit:-20]);
        make.top.equalTo(self.imgvTop.mas_bottom).offset([SFitUtil fit:20]);
    }];
    [self.lblContent mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:30]);
        make.right.mas_equalTo([SFitUtil fit:-30]);
        make.top.equalTo(self.lblTitle.mas_bottom).offset([SFitUtil fit:16]);
    }];
    [self.btnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo([SFitUtil fit:-30]);
        make.top.equalTo(self.lblContent.mas_bottom).offset([SFitUtil fit:20]);
        make.height.mas_equalTo([SFitUtil fit:44]);
        make.bottom.mas_equalTo([SFitUtil fit:-20]);
    }];
    [self.btnCancel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo([SFitUtil fit:30]);
        make.centerY.width.height.equalTo(self.btnSure);
        make.right.equalTo(self.btnSure.mas_left).offset([SFitUtil fit:-30]);
    }];
    
}

- (void)show:(UIView *)view{
    for (UIView *item in view.subviews) {
        if ([item isKindOfClass:[self class]]){
            return;
        }
    }
    [view addSubview:self];
    [view bringSubviewToFront:self];
    self.alpha = 0;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

- (void)hide{
    [self removeFromSuperview];
}

- (void)cancelClickAction{
    exit(0);
}
- (void)sureClickAction{
    [SLocalUtil setString:@"1" forKey:kshow_privacy_alert_key];
    [self hide];
}

- (void)clickAgreeUrl:(NSInteger)tag{
    [self hide];
    YMProtocolViewController *vc = [[YMProtocolViewController alloc] init];
    vc.protocolType = tag;
    vc.hidesBottomBarWhenPushed = YES;
    [SJumpUtil pushVC:vc animated:YES];
}
#pragma mark -- setter and getter
- (UIView *)vAlert{
    if (_vAlert == nil){
        _vAlert = [[UIView alloc] init];
        _vAlert.backgroundColor = [SUIUtil colorOf:0xffffff];
        _vAlert.clipsToBounds = YES;
        _vAlert.layer.cornerRadius = [SFitUtil fit:10];
    }
    return _vAlert;
}
- (UIImageView *)imgvTop{
    if (_imgvTop == nil){
        _imgvTop = [[UIImageView alloc] init];
        _imgvTop.image = [UIImage imageNamed:@"alert_privacy_icon"];
    }
    return _imgvTop;
}
- (UILabel *)lblTitle{
    if (_lblTitle == nil){
        _lblTitle = [SThemeUtil boldLabel:KLanguage(@"all_ag_txt") color:0x333333 fontSize:18];
        _lblTitle.textAlignment = NSTextAlignmentCenter;
        _lblTitle.adjustsFontSizeToFitWidth = YES;
        _lblTitle.minimumScaleFactor = 0.6;
    }
    return _lblTitle;
}
- (UIButton *)btnCancel{
    if (_btnCancel == nil) {
        _btnCancel = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnCancel setTitle:KLanguage(@"no_use_txt") forState:UIControlStateNormal];
        [_btnCancel setTitleColor:[SUIUtil colorOf:0x333333] forState:UIControlStateNormal];
        _btnCancel.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnCancel addTarget:self action:@selector(cancelClickAction) forControlEvents:UIControlEventTouchUpInside];
        _btnCancel.clipsToBounds = YES;
        _btnCancel.layer.cornerRadius = [SFitUtil fit:10];
        _btnCancel.layer.borderWidth = [SFitUtil fitLine];
        _btnCancel.layer.borderColor = [SUIUtil colorOf:0xCCCCCC].CGColor;
        _btnCancel.backgroundColor = [SUIUtil colorOf:0xffffff];
    }
    return _btnCancel;
}
- (UIButton *)btnSure{
    if (_btnSure == nil) {
        _btnSure = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnSure setTitle:KLanguage(@"agree_txt") forState:UIControlStateNormal];
        [_btnSure setTitleColor:[SUIUtil colorOf:0xffffff] forState:UIControlStateNormal];
        _btnSure.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnSure addTarget:self action:@selector(sureClickAction) forControlEvents:UIControlEventTouchUpInside];
        _btnSure.backgroundColor = [SUIUtil colorOf:0x831CB5];
        _btnSure.clipsToBounds = YES;
        _btnSure.layer.cornerRadius = [SFitUtil fit:10];
    }
    return _btnSure;
}
- (YYLabel *)lblContent{
    if (!_lblContent) {
        _lblContent = [[YYLabel alloc] init];
        _lblContent.textColor = [SUIUtil colorOf:0x666666];
        _lblContent.font = [SFitUtil fitFont:14];
        _lblContent.textAlignment = NSTextAlignmentLeft;
        _lblContent.numberOfLines = 0;
        _lblContent.preferredMaxLayoutWidth = [SFitUtil fit:330-60];
        
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:[SFitUtil fit:4]];
        NSDictionary *attDict = @{NSParagraphStyleAttributeName:paragraphStyle,NSForegroundColorAttributeName:[SUIUtil colorOf:0x666666],NSFontAttributeName:[SFitUtil fitFont:14]};
        
        NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:KLanguage(@"ag_hint_1_txt") attributes:attDict];
        NSString *readText1 = KLanguage(@"ag_hint_2_txt");
        WEAK_SELF;
        NSMutableAttributedString *read1 = [[NSMutableAttributedString alloc] initWithString:readText1 attributes:attDict];
        [read1 setTextHighlightRange:NSMakeRange(0, readText1.length) color:[SUIUtil colorOf:0x458cf5] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [weakSelf clickAgreeUrl:1];
        }];
        [att appendAttributedString:read1];
        
        NSAttributedString *attr1 = [[NSAttributedString alloc] initWithString:KLanguage(@"and_txt") attributes:attDict];
        [att appendAttributedString:attr1];
        NSString *readText2 = KLanguage(@"ag_hint_3_txt");
        NSMutableAttributedString *read2 = [[NSMutableAttributedString alloc] initWithString:readText2 attributes:attDict];
        [read2 setTextHighlightRange:NSMakeRange(0, readText2.length) color:[SUIUtil colorOf:0x458cf5] backgroundColor:[UIColor clearColor] tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            [weakSelf clickAgreeUrl:2];
        }];
        [att appendAttributedString:read2];
        
        _lblContent.attributedText = att;
        _lblContent.textAlignment = NSTextAlignmentCenter;
    }
    return _lblContent;
}
@end
